import Https from './https'
import router from '@/router/index'
import md5 from 'js-md5'
if (window.require) {
    var ipc = window.require('electron').ipcRenderer
  }
/**
 * 获取网络数据
 * @param {String} url 请求的链接,如："/static/data/layout-header-tabs.json"
 * @param {Object} params 请求的参数，如：get请求 { params:{id:5}} post请求：{id:5,name:"wang"}
 * @param {String} method 请求的方式，"get"或者"post"或者"formData",默认为"get"
 */
export const fetchData = async function (url,params,method='get') {
    let resData = null;
    if(Object.is(method,'get')){
        resData = await Https.get(url,params)
    }else if(Object.is(method,'post')){
        resData = await Https.post(url,params)
    }else{
        resData = await Https.formData(url,params)
    }

    if(resData.status){
        if(resData.status == '10006'){ //控制多端登陆
            Https.get('chat/postUpdateUserLoginStatus',{login_type:0})
            localStorage.removeItem('iszdlogin'); //清除自动登录
            location.reload(); //刷新页面，退出im服务器;
            ipc.send('resetLoginWin')
            router.push('/');
        }else{
            return resData.data;
        }
    }else{
        this.$toast({
            message: `服务器错误:${resData.message}`
        })
    }
}

/**
 * 时间戳转时间格式
 */
export const format = function(time,type){
    let unixtime = time
    let unixTimestamp = new Date(unixtime * 1000)
    let Y = unixTimestamp.getFullYear()
    let M = ((unixTimestamp.getMonth() + 1) >= 10 ? (unixTimestamp.getMonth() + 1) : '0' + (unixTimestamp.getMonth() + 1))
    let D = (unixTimestamp.getDate() >= 10 ? unixTimestamp.getDate() : '0' + unixTimestamp.getDate())
    let h = (unixTimestamp.getHours() >= 10 ? unixTimestamp.getHours() : '0'+ unixTimestamp.getHours());
    let min = (unixTimestamp.getMinutes() >= 10 ? unixTimestamp.getMinutes():'0'+unixTimestamp.getMinutes());
    let s = (unixTimestamp.getSeconds() >= 10 ? unixTimestamp.getSeconds():'0'+unixTimestamp.getSeconds());
    if(type){
        let time = `${Y}/${M}/${D} ${h}:${min}:${s}`
        return time;
    }else{
        let Time = h+':'+min;
        return Time;
    }
}


export const uuid = function(){
    　let len = len || 32;
    　let $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
    　let maxPos = $chars.length;
    　let pwd = '';
    　for (let i = 0; i < len; i++) {
    　　　pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
    　}
    let unixtimeStr;
    let unixtime = Math.round(new Date().getTime()).toString()+pwd;
    let uuidstr =  md5(unixtime);
    unixtimeStr = uuidstr.substr(0,8)+'-'+ uuidstr.substr(8,12)+'-'+ uuidstr.substr(12,16)+'-'+uuidstr.substr(16,20)+'-'+uuidstr.substr(20,32);
    return unixtimeStr;
}